<?php


namespace app\api\controller\finance;


use app\api\controller\Api;
use app\common\order\model\OrderCollect;
use app\common\order\model\OrderIncome;
use util\Format;

class Unincome extends Api
{
    public function index()
    {
        $post = $this->request->post();
       
        $where = [];
        // 收款性质
        if (!empty($post['income_type'])) $where[] = ['OrderCollect.type', 'in', $post['income_type']];
        // 收款金额
        if (!empty($post['total'])) $where[] = ['OrderCollect.price', '=', $post['total']];
        // 收款区间检索
        if (!empty($post['collect_range'])) {
            $collectRange = Format::dateRange($post['collect_range']);
            $where[] = ['OrderCollect.collect_date', 'between', $collectRange];
        }

        // 仅仅显示未收的
        if (!empty($post['is_unincome'])) {
            $where[] = ['OrderCollect.complete', '<', 100];
        }

        $hasWhere = [];
        $hasWhere[] = ['Order.refund_status', '=', 0];
        // $hasWhere[] = ['Order.order_type', '<>', 2];
        if (!empty($post['user_id'])) $hasWhere[] = ['Order.user_id', 'in', $post['user_id']];
        if (!empty($post['company_id'])) $hasWhere[] = ['Order.company_id', 'in', $post['company_id']];
        if (!empty($post['store_id'])) $hasWhere[] = ['Order.store_id', 'in', $post['store_id']];
        if (!empty($post['category_id'])) $hasWhere[] = ['Order.category_id', 'in', $post['category_id']];
        if (!empty($post['order_type'])) $hasWhere[] = ['Order.order_type', 'in', $post['order_type']];
        if (!empty($post['sign_date_range'])) {
            $signDateRange = Format::dateRange($post['sign_date_range']);
            $hasWhere[] = ['Order.sign_date', 'between', $signDateRange];
        }
        if (!empty($post['event_date_range'])) {
            $eventDateRange = Format::dateRange($post['event_date_range']);
            $hasWhere[] = ['Order.event_date', 'between', $eventDateRange];
        }
        if (!empty($post['keyword'])) $hasWhere[] = ['Order.contact_no|Order.bridegroom|Order.bridegroom_mobile|Order.bride|Order.bride_mobile', 'like', "%{$post['keyword']}%"];

        if (empty($post['is_export'])) {
            list($cfg) = Format::pagination($post);
            $model = OrderCollect::hasWhere('order', $hasWhere);
            $pg = $model->with('order')->where($where)->order('id desc')->paginate($cfg);
            $rows = $pg->getCollection();
            foreach ($rows as $row) {
                $row->append(['title', 'check_tag', 'income']);
                $row->order->append(['category', 'company', 'cooperation', 'user', 'store', 'hall', 'banquet_type', 'order_type_text', 'check_tag']);
            }

            $result = [];
            $result['list'] = $rows;
            $result['pagination'] = Format::pgResult($pg);
            return success($result);
        } else {
            $expData = [];
            $header = [
                'sign_company' => '签单公司',
                'cooperation' => '合作模式',
                'order_type' => '订单性质',
                'category' => '服务类型',
                'sales' => '签单销售',
                'bridegroom' => '新郎姓名',
                'bridegroom_mobile' => '新郎电话',
                'bride' => '新娘姓名',
                'bride_mobile' => '新娘电话',
                'event_date' => '举办日期',
                'sign_date' => '签约日期',
                'store' => '酒店名称',
                'title' => '计划收款性质',
                'price' => '计划收款金额',
                'collect_date' => '计划收款时间',
                'income_total' => '实际收款金额',
                'income_date' => '实际收款日期',
            ];

            $model = OrderCollect::hasWhere('order', $hasWhere);
            $rows = $model->with('order')->where($where)->order('id desc')->select();
            foreach ($rows as $row) {
                $row->append(['title', 'check_tag', 'income']);
                $row->order->append(['category', 'company', 'cooperation', 'user', 'store', 'hall', 'banquet_type', 'order_type_text', 'check_tag']);

                $expRow = [];
                $expRow['sign_company'] = $row->order->company['title'];
                $expRow['cooperation'] = $row->order->cooperation['title'];
                $expRow['order_type'] = $row->order->order_type_text;
                $expRow['category'] = $row->order->category['title'];
                $expRow['sales'] = $row->order->user['realname'];
                $expRow['bridegroom'] = $row->order->bridegroom;
                $expRow['bridegroom_mobile'] = $row->order->bridegroom_mobile;
                $expRow['bride'] = $row->order->bride;
                $expRow['bride_mobile'] = $row->order->bride_mobile;
                $expRow['event_date'] = $row->order->event_date;
                $expRow['sign_date'] = $row->order->sign_date;
                $expRow['store'] = $row->order->store['title'];
                $expRow['title'] = $row->title;
                $expRow['price'] = $row->price;
                $expRow['collect_date'] = $row->collect_date;
                $expRow['income_total'] = $row->income['total'];
                $expRow['income_date'] = $row->income['income_date'];
                $expData[] = $expRow;
            }

            $fileName = '【应收单】' . date('YmdHis');
            $url = \app\common\third\Excel::exportExcel($expData, $header, $fileName);
            return success(['url' => $url], '导出成功');
        }
    }

    // 我的应收单
    public function mine()
    {
        $post = $this->request->post();
        $user = $this->request->user;

        $where = [];
        // 收款性质
        if (!empty($post['income_type'])) $where[] = ['OrderCollect.type', 'in', $post['income_type']];
        // 收款金额
        if (!empty($post['total'])) $where[] = ['OrderCollect.price', '=', $post['total']];
        // 收款区间检索
        if (!empty($post['collect_range'])) {
            $collectRange = Format::dateRange($post['collect_range']);
            $where[] = ['OrderCollect.collect_date', 'between', $collectRange];
        }

        // 仅仅显示未收的
        if (!empty($post['is_unincome'])) {
            $where[] = ['OrderCollect.complete', '<', 100];
        }

        $hasWhere = [];
        $hasWhere[] = ['Order.refund_status', '=', 0];
        // $hasWhere[] = ['Order.order_type', '<>', 2];
        $hasWhere[] = ['Order.user_id', '=', $user['id']];
        if (!empty($post['company_id'])) $hasWhere[] = ['Order.company_id', 'in', $post['company_id']];
        if (!empty($post['store_id'])) $hasWhere[] = ['Order.store_id', 'in', $post['store_id']];
        if (!empty($post['category_id'])) $hasWhere[] = ['Order.category_id', 'in', $post['category_id']];
        if (!empty($post['order_type'])) $hasWhere[] = ['Order.order_type', 'in', $post['order_type']];
        if (!empty($post['sign_date_range'])) {
            $signDateRange = Format::dateRange($post['sign_date_range']);
            $hasWhere[] = ['Order.sign_date', 'between', $signDateRange];
        }
        if (!empty($post['event_date_range'])) {
            $eventDateRange = Format::dateRange($post['event_date_range']);
            $hasWhere[] = ['Order.event_date', 'between', $eventDateRange];
        }
        if (!empty($post['keyword'])) $hasWhere[] = ['Order.contact_no|Order.bridegroom|Order.bridegroom_mobile|Order.bride|Order.bride_mobile', 'like', "%{$post['keyword']}%"];

        if (empty($post['is_export'])) {
            list($cfg) = Format::pagination($post);
            $model = OrderCollect::hasWhere('order', $hasWhere);
            $pg = $model->with('order')->where($where)->order('id desc')->paginate($cfg);
            $rows = $pg->getCollection();
            foreach ($rows as $row) {
                $row->append(['title', 'check_tag', 'income']);
                $row->order->append(['category', 'company', 'cooperation', 'user', 'store', 'hall', 'banquet_type', 'order_type_text', 'check_tag']);
            }

            $result = [];
            $result['list'] = $rows;
            $result['pagination'] = Format::pgResult($pg);
            return success($result);
        } else {
            $expData = [];
            $header = [
                'sign_company' => '签单公司',
                'cooperation' => '合作模式',
                'order_type' => '订单性质',
                'category' => '服务类型',
                'sales' => '签单销售',
                'bridegroom' => '新郎姓名',
                'bridegroom_mobile' => '新郎电话',
                'bride' => '新娘姓名',
                'bride_mobile' => '新娘电话',
                'event_date' => '举办日期',
                'sign_date' => '签约日期',
                'store' => '酒店名称',
                'title' => '计划收款性质',
                'price' => '计划收款金额',
                'collect_date' => '计划收款时间',
                'income_total' => '实际收款金额',
                'income_date' => '实际收款日期',
            ];

            $model = OrderCollect::hasWhere('order', $hasWhere);
            $rows = $model->with('order')->where($where)->order('id desc')->select();
            foreach ($rows as $row) {
                $row->append(['title', 'check_tag', 'income']);
                $row->order->append(['category', 'company', 'cooperation', 'user', 'store', 'hall', 'banquet_type', 'order_type_text', 'check_tag']);

                $expRow = [];
                $expRow['sign_company'] = $row->order->company['title'];
                $expRow['cooperation'] = $row->order->cooperation['title'];
                $expRow['order_type'] = $row->order->order_type_text;
                $expRow['category'] = $row->order->category['title'];
                $expRow['sales'] = $row->order->user['realname'];
                $expRow['bridegroom'] = $row->order->bridegroom;
                $expRow['bridegroom_mobile'] = $row->order->bridegroom_mobile;
                $expRow['bride'] = $row->order->bride;
                $expRow['bride_mobile'] = $row->order->bride_mobile;
                $expRow['event_date'] = $row->order->event_date;
                $expRow['sign_date'] = $row->order->sign_date;
                $expRow['store'] = $row->order->store['title'];
                $expRow['title'] = $row->title;
                $expRow['price'] = $row->price;
                $expRow['collect_date'] = $row->collect_date;
                $expRow['income_total'] = $row->income['total'];
                $expRow['income_date'] = $row->income['income_date'];
                $expData[] = $expRow;
            }

            $fileName = '【应收单】' . date('YmdHis');
            $url = \app\common\third\Excel::exportExcel($expData, $header, $fileName);
            return success(['url' => $url], '导出成功');
        }
    }
}
